package cn.kgc.crowdfundingadmin03.controller;

import cn.kgc.crowdfundingadmin03.common.CrowdFundingConstant;

import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Admin;
import cn.kgc.crowdfundingadmin03.pojo.Advertisement;
import cn.kgc.crowdfundingadmin03.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class AdvertisementController {

    @Resource
    AdvertisementService advertisementService;

    //分页查询用
    @RequestMapping("/advertisement/query/for/search")

    public String queryForSearch(
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model){
        PageInfo<Advertisement> pageInfo = advertisementService.queryForKeywordSearch(pageNum, pageSize, keyword);

        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "advertisement";
    }


    //批量删除
    @ResponseBody
    @RequestMapping("/advertisement/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        advertisementService.batchRemove(adminIdList);
        return ResultEntity.successWithoutData();
    }


    //增加
    @RequestMapping("/advertisement/save")
    public String saveAdmin(Advertisement admin) {
        advertisementService.saveAdmin(admin);
        //删除成功跳转到查询页面  分页查询
        return "redirect:/advertisement/query/for/search";
    }

    //修改前的查询 根据id查询
    @RequestMapping("/advertisement/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Advertisement admin =advertisementService.getAdminById(adminId);

        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,admin);
        return "advertisement-edit";
    }

    @RequestMapping("/advertisement/update")
    public String updateAdmin(Advertisement admin) {
        advertisementService.updateAdmin(admin);
        //当前页 存储过去
        return "redirect:/advertisement/query/for/search" ;
    }

}
